import request from '@/api/index'
import store from '@/store/index'

// 登录
export const wxMiniLogin = {
	request: (data) => {
		return request({
			url: '/WxAppLogin/WxAppLogin',
			method: 'post',
			data: data
		})
	}
}
// 登录
export const accountLogin = {
	request: (data) => {
		return request({
			url: '/login/systemLogin?type=System',
			method: 'post',
			data: data
		})
	}
}

// 修改用户信息
export const updateUserInfo = {
	request: (params) => {
		return request({
			url: '/healthdetect/usave',
			method: 'POST',
			data: params,
			custom: {
				unRemovesKeysWithNullValues: true
			}
		})
	}
}

// 获取用户信息-用户登录（token）
export const tokenLogin = {
	request: () => {
		return request({
			url: '/healthdetect/member',
			method: 'POST',
			custom: {
				hideLoginTip: true,
				hideErrTip: true
			}
		})
	}
}

// 获取电竞对应token
export const connectGame = {
	request: () => {
		return request({
			url: '/member/connectGame',
			method: 'POST',
		})
	}
}

export function getUserInfoApi() {
	return request({
		url: '/UserInfo/queryCurrentUserInfo',
		method: 'GET',
	})
}


/**
 * 获取用户信息-用户登录（token）
 * @param id 用户id
 * @param token 用户token
 */
var loading = false;
export const checkToken = () => {
	if (loading) {
		return;
	}
	loading = true;
	if (store.state.token && store.state.token.length) {
		tokenLogin.request().then(res => {
			loading = false;
			if (uni.$u.test.object(res.data)) {
				console.log('###获取用户信息成功：', res.data);
				var userInfo = {
					...store.state.userInfo,
					...res.data
				}
				try {
					userInfo.mobile_encrypt = `${userInfo.mobile.substr(0, 3)}****${userInfo.mobile.substr(7)}`
				} catch (_) {
					userInfo.mobile_encrypt = userInfo.mobile
				}

				store.dispatch('setUserInfo', userInfo)
				uni.setStorageSync('userInfo', JSON.stringify(userInfo))

				store.dispatch('setToken', userInfo.token)
				uni.setStorageSync('token', userInfo.token)
			}
		}).catch(err => {
			console.log('###获取用户信息失败：', err);
			loading = false;
		})
	} else {
		loading = false;
	}
}

/**
 * 获取终端客户下拉框
 * @param {*} data 
 * @returns
 */
export function QueryTerminalUserSelectApi(data) {
	return request({
		url: '/TerminalUser/queryTerminalUserSelect',
		method: 'POST',
		data
	})
}

/**
 * 修改用户信息
 * @param {*} data 
 * @returns
 */
export function UpdateUserInfoApi(data) {
	return request({
		url: '/UserInfo/updateUserInfo',
		method: 'POST',
		data
	})
}
/**
 * 用户信息下拉框
 * @param {*} data 
 * @returns
 */
export function queryUserInfoSelect(data) {
	return request({
		url: '/UserInfo/queryUserInfoSelect',
		method: 'POST',
		data
	})
}

/**
 * 获取待审批数量
 * @param {*} data 
 * @returns 
 */
export function queryBeApproveCount(data) {
	return request({
		url: '/approveProcess/queryBeApproveCount',
		method: 'POST',
		data
	})
}
/**
 * 获取流程推进数量
 * @param {*} data 
 * @returns 
 */
export function queryProcessCount(data) {
	return request({
		url: '/processNode/queryProcessCount',
		method: 'POST',
		data
	})
}
/**
 * 获取逾期数量
 * @param {*} data 
 * @returns 
 */
export function queryOverdueCount(data) {
	return request({
		url: '/processNode/queryOverdueCount',
		method: 'POST',
		data
	})
}
